{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "c10ce2ec",
   "metadata": {},
   "source": [
    "# 在 Python 中使用 Ollama API\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ecb7c3de",
   "metadata": {},
   "source": [
    "## 导入Ollama模块\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a6001d8d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Requirement already satisfied: ollama in d:\\install\\miniconda\\envs\\ollama\\lib\\site-packages (0.3.1)\n",
      "Requirement already satisfied: httpx<0.28.0,>=0.27.0 in d:\\install\\miniconda\\envs\\ollama\\lib\\site-packages (from ollama) (0.27.0)\n",
      "Requirement already satisfied: anyio in d:\\install\\miniconda\\envs\\ollama\\lib\\site-packages (from httpx<0.28.0,>=0.27.0->ollama) (4.4.0)\n",
      "Requirement already satisfied: certifi in d:\\install\\miniconda\\envs\\ollama\\lib\\site-packages (from httpx<0.28.0,>=0.27.0->ollama) (2024.7.4)\n",
      "Requirement already satisfied: httpcore==1.* in d:\\install\\miniconda\\envs\\ollama\\lib\\site-packages (from httpx<0.28.0,>=0.27.0->ollama) (1.0.5)\n",
      "Requirement already satisfied: idna in d:\\install\\miniconda\\envs\\ollama\\lib\\site-packages (from httpx<0.28.0,>=0.27.0->ollama) (3.7)\n",
      "Requirement already satisfied: sniffio in d:\\install\\miniconda\\envs\\ollama\\lib\\site-packages (from httpx<0.28.0,>=0.27.0->ollama) (1.3.1)\n",
      "Requirement already satisfied: h11<0.15,>=0.13 in d:\\install\\miniconda\\envs\\ollama\\lib\\site-packages (from httpcore==1.*->httpx<0.28.0,>=0.27.0->ollama) (0.14.0)\n",
      "Note: you may need to restart the kernel to use updated packages.\n"
     ]
    }
   ],
   "source": [
    "pip install ollama"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "19580fd0",
   "metadata": {},
   "source": [
    "## 使用方法\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "fbdf15d2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "天空看起来是蓝色的，因为太阳光线散射的现象。白日时，大气层中所有能吸收和散射光照的气体和微粒，均有作用于白天太阳光。光在经过地球大气层时，由于与各种介质相互作用（反弹、折射、散射等），因此，光线会随着角度的改变而发生偏移。这也是为什么我们天空中能看到美丽的彩虹。\n",
      "\n",
      "蓝光具有较小的波长，使它能够更深地穿透大气层，并且与大气层中的水蒸气、CO2等气体和微粒相互作用时，会被散射出来。由于我们的视觉系统对紫外线的灵敏度最低，同时蓝色在全能感受范围内位于视觉光谱中较高的一端，因此我们观察到的天空颜色主要是蓝色的（虽然在实际上，我们还可以观察到一些黄色、白色等其他颜色）。\n"
     ]
    }
   ],
   "source": [
    "import ollama\n",
    "response = ollama.chat(model='llama3.1', messages=[\n",
    "  {\n",
    "    'role': 'user',\n",
    "    'content': '为什么天空是蓝色的？',\n",
    "  },\n",
    "])\n",
    "print(response['message']['content'])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "b9f8f4a4",
   "metadata": {},
   "source": [
    "## 流式响应\n",
    "\n",
    "可以通过设置 `stream=True` 启用响应流，使函数调用返回一个 Python 生成器，其中每个部分都是流中的一个对象。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "24c0fab6",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "天空看起来是蓝色的，因为我们看到地球大气中散射的光照。这是一个复杂的问题，但可以用简单的语言解释。\n",
      "\n",
      "大气中的分子和气体会吸收或折射白色光，反射出蓝紫色的光。这个过程称为“Rayleigh 散射”。它是由于大气中的微小颗粒在不同波长下的光照表现出的差异而产生的。结果是，我们看到一个天空呈现蓝色的颜色。"
     ]
    }
   ],
   "source": [
    "import ollama\n",
    "\n",
    "stream = ollama.chat(\n",
    "    model='llama3.1',\n",
    "    messages=[{'role': 'user', 'content': '为什么天空是蓝色的？'}],\n",
    "    stream=True,\n",
    ")\n",
    "\n",
    "for chunk in stream:\n",
    "  print(chunk['message']['content'], end='', flush=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f50f5286",
   "metadata": {},
   "source": [
    "## API\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34fdd2c4",
   "metadata": {},
   "source": [
    "### 聊天\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "55166f21",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'model': 'llama3.1',\n",
       " 'created_at': '2024-08-09T09:39:13.853693892Z',\n",
       " 'message': {'role': 'assistant', 'content': '答案：因为大气散射。'},\n",
       " 'done_reason': 'stop',\n",
       " 'done': True,\n",
       " 'total_duration': 567149901,\n",
       " 'load_duration': 10989019,\n",
       " 'prompt_eval_count': 18,\n",
       " 'prompt_eval_duration': 314188000,\n",
       " 'eval_count': 9,\n",
       " 'eval_duration': 185421000}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ollama.chat(model='llama3.1', messages=[{'role': 'user', 'content': '为什么天空是蓝色的？'}])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6b3aac0e",
   "metadata": {},
   "source": [
    "### 生成\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "25d7a249",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'model': 'llama3.1',\n",
       " 'created_at': '2024-08-09T09:39:44.737161632Z',\n",
       " 'response': '蓝天白云是我们心目中的理想世界。蓝色是那么靓丽、那样的舒适，所以人们常说：蓝天白云好生活。这不是什么哲学道理，它只是自然的真实写照。有研究指出，看到蓝色的效果，是由于蓝色能起到放松人的作用，而这种情感状态，直接关系到我们的心理健康。\\n\\n\\u3000\\u3000那么为什么是蓝色呢？我们都知道地球上水的大部分是青色的，这就是为什么天空看起来是蓝色的原因了。太阳光通过大气时，与地面上的水分子相遇，并被吸收、散射和折射。这就是为什么蓝天白云这样的美景在我们的视觉中出现的缘故。\\n\\n\\u3000\\u3000地球上有70%以上的表面积都是海洋，所以蓝色霸占了自然界，给我们带来无限舒适感。所以蓝色的风景，是一种心灵的救赎。',\n",
       " 'done': True,\n",
       " 'done_reason': 'stop',\n",
       " 'context': [128009,\n",
       "  128006,\n",
       "  882,\n",
       "  128007,\n",
       "  271,\n",
       "  113221,\n",
       "  36827,\n",
       "  35894,\n",
       "  21043,\n",
       "  115427,\n",
       "  118458,\n",
       "  11571,\n",
       "  128009,\n",
       "  128006,\n",
       "  78191,\n",
       "  128007,\n",
       "  271,\n",
       "  115427,\n",
       "  36827,\n",
       "  101828,\n",
       "  103458,\n",
       "  21043,\n",
       "  98739,\n",
       "  64209,\n",
       "  30832,\n",
       "  105363,\n",
       "  22649,\n",
       "  101067,\n",
       "  102616,\n",
       "  1811,\n",
       "  115427,\n",
       "  39135,\n",
       "  21043,\n",
       "  111498,\n",
       "  21688,\n",
       "  241,\n",
       "  107921,\n",
       "  5486,\n",
       "  100823,\n",
       "  119938,\n",
       "  120178,\n",
       "  108562,\n",
       "  108905,\n",
       "  127198,\n",
       "  40053,\n",
       "  37687,\n",
       "  5232,\n",
       "  115427,\n",
       "  36827,\n",
       "  101828,\n",
       "  103458,\n",
       "  53901,\n",
       "  104654,\n",
       "  110477,\n",
       "  103668,\n",
       "  101879,\n",
       "  115981,\n",
       "  48864,\n",
       "  45893,\n",
       "  22649,\n",
       "  115973,\n",
       "  111465,\n",
       "  109683,\n",
       "  9554,\n",
       "  89151,\n",
       "  41073,\n",
       "  62543,\n",
       "  102867,\n",
       "  1811,\n",
       "  19361,\n",
       "  102473,\n",
       "  64467,\n",
       "  20834,\n",
       "  3922,\n",
       "  110354,\n",
       "  115427,\n",
       "  118458,\n",
       "  119881,\n",
       "  105187,\n",
       "  116382,\n",
       "  115427,\n",
       "  39135,\n",
       "  27327,\n",
       "  72718,\n",
       "  28037,\n",
       "  54322,\n",
       "  104500,\n",
       "  105390,\n",
       "  113266,\n",
       "  104122,\n",
       "  106880,\n",
       "  40474,\n",
       "  99750,\n",
       "  45191,\n",
       "  3922,\n",
       "  109467,\n",
       "  110275,\n",
       "  28037,\n",
       "  98739,\n",
       "  122935,\n",
       "  22649,\n",
       "  113614,\n",
       "  3490,\n",
       "  23249,\n",
       "  23249,\n",
       "  111498,\n",
       "  113221,\n",
       "  21043,\n",
       "  115427,\n",
       "  39135,\n",
       "  104586,\n",
       "  11571,\n",
       "  98739,\n",
       "  72368,\n",
       "  104105,\n",
       "  127960,\n",
       "  17905,\n",
       "  53610,\n",
       "  112914,\n",
       "  105494,\n",
       "  21043,\n",
       "  103271,\n",
       "  118458,\n",
       "  103138,\n",
       "  103179,\n",
       "  113221,\n",
       "  36827,\n",
       "  35894,\n",
       "  52030,\n",
       "  106155,\n",
       "  21043,\n",
       "  115427,\n",
       "  118458,\n",
       "  113385,\n",
       "  35287,\n",
       "  1811,\n",
       "  101402,\n",
       "  83175,\n",
       "  101426,\n",
       "  68438,\n",
       "  27384,\n",
       "  102146,\n",
       "  13646,\n",
       "  126240,\n",
       "  30590,\n",
       "  28190,\n",
       "  106583,\n",
       "  53610,\n",
       "  17620,\n",
       "  45829,\n",
       "  50021,\n",
       "  110053,\n",
       "  91495,\n",
       "  101307,\n",
       "  107246,\n",
       "  51109,\n",
       "  5486,\n",
       "  107471,\n",
       "  105644,\n",
       "  34208,\n",
       "  108881,\n",
       "  105644,\n",
       "  110477,\n",
       "  103179,\n",
       "  113221,\n",
       "  115427,\n",
       "  36827,\n",
       "  101828,\n",
       "  103458,\n",
       "  118498,\n",
       "  58666,\n",
       "  86354,\n",
       "  19000,\n",
       "  125383,\n",
       "  58552,\n",
       "  103649,\n",
       "  16325,\n",
       "  111935,\n",
       "  9554,\n",
       "  120462,\n",
       "  104604,\n",
       "  3490,\n",
       "  23249,\n",
       "  23249,\n",
       "  127960,\n",
       "  17905,\n",
       "  19361,\n",
       "  2031,\n",
       "  4,\n",
       "  71689,\n",
       "  9554,\n",
       "  21405,\n",
       "  122858,\n",
       "  108619,\n",
       "  56235,\n",
       "  106390,\n",
       "  108905,\n",
       "  115427,\n",
       "  39135,\n",
       "  52597,\n",
       "  116,\n",
       "  108928,\n",
       "  35287,\n",
       "  109683,\n",
       "  98220,\n",
       "  3922,\n",
       "  90112,\n",
       "  98739,\n",
       "  103652,\n",
       "  37507,\n",
       "  43292,\n",
       "  48249,\n",
       "  120178,\n",
       "  108562,\n",
       "  99750,\n",
       "  1811,\n",
       "  105123,\n",
       "  115427,\n",
       "  118458,\n",
       "  103125,\n",
       "  86354,\n",
       "  105187,\n",
       "  120143,\n",
       "  64209,\n",
       "  108298,\n",
       "  9554,\n",
       "  108057,\n",
       "  28425,\n",
       "  236,\n",
       "  1811],\n",
       " 'total_duration': 5232590691,\n",
       " 'load_duration': 19222289,\n",
       " 'prompt_eval_count': 18,\n",
       " 'prompt_eval_duration': 194760000,\n",
       " 'eval_count': 213,\n",
       " 'eval_duration': 4868250000}"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ollama.generate(model='llama3.1', prompt='为什么天空是蓝色的？')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "420214aa",
   "metadata": {},
   "source": [
    "### 本地模型列表\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "8389ffbd",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'models': [{'name': 'llava:latest',\n",
       "   'model': 'llava:latest',\n",
       "   'modified_at': '2024-08-08T14:20:09.31045753+08:00',\n",
       "   'size': 4733363377,\n",
       "   'digest': '8dd30f6b0cb19f555f2c7a7ebda861449ea2cc76bf1f44e262931f45fc81d081',\n",
       "   'details': {'parent_model': '',\n",
       "    'format': 'gguf',\n",
       "    'family': 'llama',\n",
       "    'families': ['llama', 'clip'],\n",
       "    'parameter_size': '7B',\n",
       "    'quantization_level': 'Q4_0'}},\n",
       "  {'name': 'llama3.1:latest',\n",
       "   'model': 'llama3.1:latest',\n",
       "   'modified_at': '2024-08-08T14:13:55.590492785+08:00',\n",
       "   'size': 4661230977,\n",
       "   'digest': '75382d0899dfaaa6ce331cf680b72bd6812c7f05e5158c5f2f43c6383e21d734',\n",
       "   'details': {'parent_model': '',\n",
       "    'format': 'gguf',\n",
       "    'family': 'llama',\n",
       "    'families': ['llama'],\n",
       "    'parameter_size': '8.0B',\n",
       "    'quantization_level': 'Q4_0'}},\n",
       "  {'name': 'glm4xiuxian:latest',\n",
       "   'model': 'glm4xiuxian:latest',\n",
       "   'modified_at': '2024-08-07T16:16:37.744493536+08:00',\n",
       "   'size': 4661231198,\n",
       "   'digest': '348c4463f081b362ef39ef448ff73a1674f9cf55c703de0883c64afbd8cf34c6',\n",
       "   'details': {'parent_model': '',\n",
       "    'format': 'gguf',\n",
       "    'family': 'llama',\n",
       "    'families': ['llama'],\n",
       "    'parameter_size': '8.0B',\n",
       "    'quantization_level': 'Q4_0'}}]}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ollama.list()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e85e25a6",
   "metadata": {},
   "source": [
    "### 显示模型信息\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "43dd7845",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'license': 'LLAMA 3.1 COMMUNITY LICENSE AGREEMENT\\nLlama 3.1 Version Release Date: July 23, 2024\\n\\n“Agreement” means the terms and conditions for use, reproduction, distribution and modification of the\\nLlama Materials set forth herein.\\n\\n“Documentation” means the specifications, manuals and documentation accompanying Llama 3.1\\ndistributed by Meta at https://llama.meta.com/doc/overview.\\n\\n“Licensee” or “you” means you, or your employer or any other person or entity (if you are entering into\\nthis Agreement on such person or entity’s behalf), of the age required under applicable laws, rules or\\nregulations to provide legal consent and that has legal authority to bind your employer or such other\\nperson or entity if you are entering in this Agreement on their behalf.\\n\\n“Llama 3.1” means the foundational large language models and software and algorithms, including\\nmachine-learning model code, trained model weights, inference-enabling code, training-enabling code,\\nfine-tuning enabling code and other elements of the foregoing distributed by Meta at\\nhttps://llama.meta.com/llama-downloads.\\n\\n“Llama Materials” means, collectively, Meta’s proprietary Llama 3.1 and Documentation (and any\\nportion thereof) made available under this Agreement.\\n\\n“Meta” or “we” means Meta Platforms Ireland Limited (if you are located in or, if you are an entity, your\\nprincipal place of business is in the EEA or Switzerland) and Meta Platforms, Inc. (if you are located\\noutside of the EEA or Switzerland).\\n\\nBy clicking “I Accept” below or by using or distributing any portion or element of the Llama Materials,\\nyou agree to be bound by this Agreement.\\n\\n1. License Rights and Redistribution.\\n\\n  a. Grant of Rights. You are granted a non-exclusive, worldwide, non-transferable and royalty-free\\nlimited license under Meta’s intellectual property or other rights owned by Meta embodied in the Llama\\nMaterials to use, reproduce, distribute, copy, create derivative works of, and make modifications to the\\nLlama Materials.\\n\\n  b. Redistribution and Use.\\n\\n      i. If you distribute or make available the Llama Materials (or any derivative works\\nthereof), or a product or service (including another AI model) that contains any of them, you shall (A)\\nprovide a copy of this Agreement with any such Llama Materials; and (B) prominently display “Built with\\nLlama” on a related website, user interface, blogpost, about page, or product documentation. If you use\\nthe Llama Materials or any outputs or results of the Llama Materials to create, train, fine tune, or\\notherwise improve an AI model, which is distributed or made available, you shall also include “Llama” at\\nthe beginning of any such AI model name.\\n\\n      ii. If you receive Llama Materials, or any derivative works thereof, from a Licensee as part \\nof an integrated end user product, then Section 2 of this Agreement will not apply to you.\\n\\n      iii. You must retain in all copies of the Llama Materials that you distribute the following\\nattribution notice within a “Notice” text file distributed as a part of such copies: “Llama 3.1 is\\nlicensed under the Llama 3.1 Community License, Copyright © Meta Platforms, Inc. All Rights\\nReserved.”\\n\\n      iv. Your use of the Llama Materials must comply with applicable laws and regulations\\n(including trade compliance laws and regulations) and adhere to the Acceptable Use Policy for the Llama\\nMaterials (available at https://llama.meta.com/llama3_1/use-policy), which is hereby incorporated by\\nreference into this Agreement.\\n\\n2. Additional Commercial Terms. If, on the Llama 3.1 version release date, the monthly active users\\nof the products or services made available by or for Licensee, or Licensee’s affiliates, is greater than 700\\nmillion monthly active users in the preceding calendar month, you must request a license from Meta,\\nwhich Meta may grant to you in its sole discretion, and you are not authorized to exercise any of the\\nrights under this Agreement unless or until Meta otherwise expressly grants you such rights.\\n\\n3. Disclaimer of Warranty. UNLESS REQUIRED BY APPLICABLE LAW, THE LLAMA MATERIALS AND ANY\\nOUTPUT AND RESULTS THEREFROM ARE PROVIDED ON AN “AS IS” BASIS, WITHOUT WARRANTIES OF\\nANY KIND, AND META DISCLAIMS ALL WARRANTIES OF ANY KIND, BOTH EXPRESS AND IMPLIED,\\nINCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OF TITLE, NON-INFRINGEMENT,\\nMERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. YOU ARE SOLELY RESPONSIBLE FOR\\nDETERMINING THE APPROPRIATENESS OF USING OR REDISTRIBUTING THE LLAMA MATERIALS AND\\nASSUME ANY RISKS ASSOCIATED WITH YOUR USE OF THE LLAMA MATERIALS AND ANY OUTPUT AND\\nRESULTS.\\n\\n4. Limitation of Liability. IN NO EVENT WILL META OR ITS AFFILIATES BE LIABLE UNDER ANY THEORY OF\\nLIABILITY, WHETHER IN CONTRACT, TORT, NEGLIGENCE, PRODUCTS LIABILITY, OR OTHERWISE, ARISING\\nOUT OF THIS AGREEMENT, FOR ANY LOST PROFITS OR ANY INDIRECT, SPECIAL, CONSEQUENTIAL,\\nINCIDENTAL, EXEMPLARY OR PUNITIVE DAMAGES, EVEN IF META OR ITS AFFILIATES HAVE BEEN ADVISED\\nOF THE POSSIBILITY OF ANY OF THE FOREGOING.\\n\\n5. Intellectual Property.\\n\\n  a. No trademark licenses are granted under this Agreement, and in connection with the Llama\\nMaterials, neither Meta nor Licensee may use any name or mark owned by or associated with the other\\nor any of its affiliates, except as required for reasonable and customary use in describing and\\nredistributing the Llama Materials or as set forth in this Section 5(a). Meta hereby grants you a license to\\nuse “Llama” (the “Mark”) solely as required to comply with the last sentence of Section 1.b.i. You will\\ncomply with Meta’s brand guidelines (currently accessible at\\nhttps://about.meta.com/brand/resources/meta/company-brand/ ). All goodwill arising out of your use\\nof the Mark will inure to the benefit of Meta.\\n\\n  b. Subject to Meta’s ownership of Llama Materials and derivatives made by or for Meta, with\\nrespect to any derivative works and modifications of the Llama Materials that are made by you, as\\nbetween you and Meta, you are and will be the owner of such derivative works and modifications.\\n\\n  c. If you institute litigation or other proceedings against Meta or any entity (including a\\ncross-claim or counterclaim in a lawsuit) alleging that the Llama Materials or Llama 3.1 outputs or\\nresults, or any portion of any of the foregoing, constitutes infringement of intellectual property or other\\nrights owned or licensable by you, then any licenses granted to you under this Agreement shall\\nterminate as of the date such litigation or claim is filed or instituted. You will indemnify and hold\\nharmless Meta from and against any claim by any third party arising out of or related to your use or\\ndistribution of the Llama Materials.\\n\\n6. Term and Termination. The term of this Agreement will commence upon your acceptance of this\\nAgreement or access to the Llama Materials and will continue in full force and effect until terminated in\\naccordance with the terms and conditions herein. Meta may terminate this Agreement if you are in\\nbreach of any term or condition of this Agreement. Upon termination of this Agreement, you shall delete\\nand cease use of the Llama Materials. Sections 3, 4 and 7 shall survive the termination of this\\nAgreement.\\n\\n7. Governing Law and Jurisdiction. This Agreement will be governed and construed under the laws of\\nthe State of California without regard to choice of law principles, and the UN Convention on Contracts\\nfor the International Sale of Goods does not apply to this Agreement. The courts of California shall have\\nexclusive jurisdiction of any dispute arising out of this Agreement.\\n\\n# Llama 3.1 Acceptable Use Policy\\n\\nMeta is committed to promoting safe and fair use of its tools and features, including Llama 3.1. If you\\naccess or use Llama 3.1, you agree to this Acceptable Use Policy (“Policy”). The most recent copy of\\nthis policy can be found at [https://llama.meta.com/llama3_1/use-policy](https://llama.meta.com/llama3_1/use-policy)\\n\\n## Prohibited Uses\\n\\nWe want everyone to use Llama 3.1 safely and responsibly. You agree you will not use, or allow\\nothers to use, Llama 3.1 to:\\n\\n1. Violate the law or others’ rights, including to:\\n    1. Engage in, promote, generate, contribute to, encourage, plan, incite, or further illegal or unlawful activity or content, such as:\\n        1. Violence or terrorism\\n        2. Exploitation or harm to children, including the solicitation, creation, acquisition, or dissemination of child exploitative content or failure to report Child Sexual Abuse Material\\n        3. Human trafficking, exploitation, and sexual violence\\n        4. The illegal distribution of information or materials to minors, including obscene materials, or failure to employ legally required age-gating in connection with such information or materials.\\n        5. Sexual solicitation\\n        6. Any other criminal activity\\n    3. Engage in, promote, incite, or facilitate the harassment, abuse, threatening, or bullying of individuals or groups of individuals\\n    4. Engage in, promote, incite, or facilitate discrimination or other unlawful or harmful conduct in the provision of employment, employment benefits, credit, housing, other economic benefits, or other essential goods and services\\n    5. Engage in the unauthorized or unlicensed practice of any profession including, but not limited to, financial, legal, medical/health, or related professional practices\\n    6. Collect, process, disclose, generate, or infer health, demographic, or other sensitive personal or private information about individuals without rights and consents required by applicable laws\\n    7. Engage in or facilitate any action or generate any content that infringes, misappropriates, or otherwise violates any third-party rights, including the outputs or results of any products or services using the Llama Materials\\n    8. Create, generate, or facilitate the creation of malicious code, malware, computer viruses or do anything else that could disable, overburden, interfere with or impair the proper working, integrity, operation or appearance of a website or computer system\\n\\n2. Engage in, promote, incite, facilitate, or assist in the planning or development of activities that present a risk of death or bodily harm to individuals, including use of Llama 3.1 related to the following:\\n    1. Military, warfare, nuclear industries or applications, espionage, use for materials or activities that are subject to the International Traffic Arms Regulations (ITAR) maintained by the United States Department of State\\n    2. Guns and illegal weapons (including weapon development)\\n    3. Illegal drugs and regulated/controlled substances\\n    4. Operation of critical infrastructure, transportation technologies, or heavy machinery\\n    5. Self-harm or harm to others, including suicide, cutting, and eating disorders\\n    6. Any content intended to incite or promote violence, abuse, or any infliction of bodily harm to an individual\\n\\n3. Intentionally deceive or mislead others, including use of Llama 3.1 related to the following:\\n    1. Generating, promoting, or furthering fraud or the creation or promotion of disinformation\\n    2. Generating, promoting, or furthering defamatory content, including the creation of defamatory statements, images, or other content\\n    3. Generating, promoting, or further distributing spam\\n    4. Impersonating another individual without consent, authorization, or legal right\\n    5. Representing that the use of Llama 3.1 or outputs are human-generated\\n    6. Generating or facilitating false online engagement, including fake reviews and other means of fake online engagement\\n\\n4. Fail to appropriately disclose to end users any known dangers of your AI system\\n\\nPlease report any violation of this Policy, software “bug,” or other problems that could lead to a violation\\nof this Policy through one of the following means:\\n\\n* Reporting issues with the model: [https://github.com/meta-llama/llama-models/issues](https://github.com/meta-llama/llama-models/issues)\\n* Reporting risky content generated by the model: developers.facebook.com/llama_output_feedback\\n* Reporting bugs and security concerns: facebook.com/whitehat/info\\n* Reporting violations of the Acceptable Use Policy or unlicensed uses of Llama 3.1: LlamaUseReport@meta.com',\n",
       " 'modelfile': '# Modelfile generated by \"ollama show\"\\n# To build a new Modelfile based on this, replace FROM with:\\n# FROM llama3.1:latest\\n\\nFROM /usr/share/ollama/.ollama/models/blobs/sha256-4f6dc812262ac5e1a74791c2a86310ebba1aa1804fa3cd1c216f5547a620d2f2\\nTEMPLATE \"\"\"{{ if .Messages }}\\n{{- if or .System .Tools }}<|start_header_id|>system<|end_header_id|>\\n{{- if .System }}\\n\\n{{ .System }}\\n{{- end }}\\n{{- if .Tools }}\\n\\nYou are a helpful assistant with tool calling capabilities. When you receive a tool call response, use the output to format an answer to the orginal use question.\\n{{- end }}\\n{{- end }}<|eot_id|>\\n{{- range $i, $_ := .Messages }}\\n{{- $last := eq (len (slice $.Messages $i)) 1 }}\\n{{- if eq .Role \"user\" }}<|start_header_id|>user<|end_header_id|>\\n{{- if and $.Tools $last }}\\n\\nGiven the following functions, please respond with a JSON for a function call with its proper arguments that best answers the given prompt.\\n\\nRespond in the format {\"name\": function name, \"parameters\": dictionary of argument name and its value}. Do not use variables.\\n\\n{{ $.Tools }}\\n{{- end }}\\n\\n{{ .Content }}<|eot_id|>{{ if $last }}<|start_header_id|>assistant<|end_header_id|>\\n\\n{{ end }}\\n{{- else if eq .Role \"assistant\" }}<|start_header_id|>assistant<|end_header_id|>\\n{{- if .ToolCalls }}\\n\\n{{- range .ToolCalls }}{\"name\": \"{{ .Function.Name }}\", \"parameters\": {{ .Function.Arguments }}}{{ end }}\\n{{- else }}\\n\\n{{ .Content }}{{ if not $last }}<|eot_id|>{{ end }}\\n{{- end }}\\n{{- else if eq .Role \"tool\" }}<|start_header_id|>ipython<|end_header_id|>\\n\\n{{ .Content }}<|eot_id|>{{ if $last }}<|start_header_id|>assistant<|end_header_id|>\\n\\n{{ end }}\\n{{- end }}\\n{{- end }}\\n{{- else }}\\n{{- if .System }}<|start_header_id|>system<|end_header_id|>\\n\\n{{ .System }}<|eot_id|>{{ end }}{{ if .Prompt }}<|start_header_id|>user<|end_header_id|>\\n\\n{{ .Prompt }}<|eot_id|>{{ end }}<|start_header_id|>assistant<|end_header_id|>\\n\\n{{ end }}{{ .Response }}{{ if .Response }}<|eot_id|>{{ end }}\"\"\"\\nPARAMETER stop <|start_header_id|>\\nPARAMETER stop <|end_header_id|>\\nPARAMETER stop <|eot_id|>\\nLICENSE \"LLAMA 3.1 COMMUNITY LICENSE AGREEMENT\\nLlama 3.1 Version Release Date: July 23, 2024\\n\\n“Agreement” means the terms and conditions for use, reproduction, distribution and modification of the\\nLlama Materials set forth herein.\\n\\n“Documentation” means the specifications, manuals and documentation accompanying Llama 3.1\\ndistributed by Meta at https://llama.meta.com/doc/overview.\\n\\n“Licensee” or “you” means you, or your employer or any other person or entity (if you are entering into\\nthis Agreement on such person or entity’s behalf), of the age required under applicable laws, rules or\\nregulations to provide legal consent and that has legal authority to bind your employer or such other\\nperson or entity if you are entering in this Agreement on their behalf.\\n\\n“Llama 3.1” means the foundational large language models and software and algorithms, including\\nmachine-learning model code, trained model weights, inference-enabling code, training-enabling code,\\nfine-tuning enabling code and other elements of the foregoing distributed by Meta at\\nhttps://llama.meta.com/llama-downloads.\\n\\n“Llama Materials” means, collectively, Meta’s proprietary Llama 3.1 and Documentation (and any\\nportion thereof) made available under this Agreement.\\n\\n“Meta” or “we” means Meta Platforms Ireland Limited (if you are located in or, if you are an entity, your\\nprincipal place of business is in the EEA or Switzerland) and Meta Platforms, Inc. (if you are located\\noutside of the EEA or Switzerland).\\n\\nBy clicking “I Accept” below or by using or distributing any portion or element of the Llama Materials,\\nyou agree to be bound by this Agreement.\\n\\n1. License Rights and Redistribution.\\n\\n  a. Grant of Rights. You are granted a non-exclusive, worldwide, non-transferable and royalty-free\\nlimited license under Meta’s intellectual property or other rights owned by Meta embodied in the Llama\\nMaterials to use, reproduce, distribute, copy, create derivative works of, and make modifications to the\\nLlama Materials.\\n\\n  b. Redistribution and Use.\\n\\n      i. If you distribute or make available the Llama Materials (or any derivative works\\nthereof), or a product or service (including another AI model) that contains any of them, you shall (A)\\nprovide a copy of this Agreement with any such Llama Materials; and (B) prominently display “Built with\\nLlama” on a related website, user interface, blogpost, about page, or product documentation. If you use\\nthe Llama Materials or any outputs or results of the Llama Materials to create, train, fine tune, or\\notherwise improve an AI model, which is distributed or made available, you shall also include “Llama” at\\nthe beginning of any such AI model name.\\n\\n      ii. If you receive Llama Materials, or any derivative works thereof, from a Licensee as part \\nof an integrated end user product, then Section 2 of this Agreement will not apply to you.\\n\\n      iii. You must retain in all copies of the Llama Materials that you distribute the following\\nattribution notice within a “Notice” text file distributed as a part of such copies: “Llama 3.1 is\\nlicensed under the Llama 3.1 Community License, Copyright © Meta Platforms, Inc. All Rights\\nReserved.”\\n\\n      iv. Your use of the Llama Materials must comply with applicable laws and regulations\\n(including trade compliance laws and regulations) and adhere to the Acceptable Use Policy for the Llama\\nMaterials (available at https://llama.meta.com/llama3_1/use-policy), which is hereby incorporated by\\nreference into this Agreement.\\n\\n2. Additional Commercial Terms. If, on the Llama 3.1 version release date, the monthly active users\\nof the products or services made available by or for Licensee, or Licensee’s affiliates, is greater than 700\\nmillion monthly active users in the preceding calendar month, you must request a license from Meta,\\nwhich Meta may grant to you in its sole discretion, and you are not authorized to exercise any of the\\nrights under this Agreement unless or until Meta otherwise expressly grants you such rights.\\n\\n3. Disclaimer of Warranty. UNLESS REQUIRED BY APPLICABLE LAW, THE LLAMA MATERIALS AND ANY\\nOUTPUT AND RESULTS THEREFROM ARE PROVIDED ON AN “AS IS” BASIS, WITHOUT WARRANTIES OF\\nANY KIND, AND META DISCLAIMS ALL WARRANTIES OF ANY KIND, BOTH EXPRESS AND IMPLIED,\\nINCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OF TITLE, NON-INFRINGEMENT,\\nMERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. YOU ARE SOLELY RESPONSIBLE FOR\\nDETERMINING THE APPROPRIATENESS OF USING OR REDISTRIBUTING THE LLAMA MATERIALS AND\\nASSUME ANY RISKS ASSOCIATED WITH YOUR USE OF THE LLAMA MATERIALS AND ANY OUTPUT AND\\nRESULTS.\\n\\n4. Limitation of Liability. IN NO EVENT WILL META OR ITS AFFILIATES BE LIABLE UNDER ANY THEORY OF\\nLIABILITY, WHETHER IN CONTRACT, TORT, NEGLIGENCE, PRODUCTS LIABILITY, OR OTHERWISE, ARISING\\nOUT OF THIS AGREEMENT, FOR ANY LOST PROFITS OR ANY INDIRECT, SPECIAL, CONSEQUENTIAL,\\nINCIDENTAL, EXEMPLARY OR PUNITIVE DAMAGES, EVEN IF META OR ITS AFFILIATES HAVE BEEN ADVISED\\nOF THE POSSIBILITY OF ANY OF THE FOREGOING.\\n\\n5. Intellectual Property.\\n\\n  a. No trademark licenses are granted under this Agreement, and in connection with the Llama\\nMaterials, neither Meta nor Licensee may use any name or mark owned by or associated with the other\\nor any of its affiliates, except as required for reasonable and customary use in describing and\\nredistributing the Llama Materials or as set forth in this Section 5(a). Meta hereby grants you a license to\\nuse “Llama” (the “Mark”) solely as required to comply with the last sentence of Section 1.b.i. You will\\ncomply with Meta’s brand guidelines (currently accessible at\\nhttps://about.meta.com/brand/resources/meta/company-brand/ ). All goodwill arising out of your use\\nof the Mark will inure to the benefit of Meta.\\n\\n  b. Subject to Meta’s ownership of Llama Materials and derivatives made by or for Meta, with\\nrespect to any derivative works and modifications of the Llama Materials that are made by you, as\\nbetween you and Meta, you are and will be the owner of such derivative works and modifications.\\n\\n  c. If you institute litigation or other proceedings against Meta or any entity (including a\\ncross-claim or counterclaim in a lawsuit) alleging that the Llama Materials or Llama 3.1 outputs or\\nresults, or any portion of any of the foregoing, constitutes infringement of intellectual property or other\\nrights owned or licensable by you, then any licenses granted to you under this Agreement shall\\nterminate as of the date such litigation or claim is filed or instituted. You will indemnify and hold\\nharmless Meta from and against any claim by any third party arising out of or related to your use or\\ndistribution of the Llama Materials.\\n\\n6. Term and Termination. The term of this Agreement will commence upon your acceptance of this\\nAgreement or access to the Llama Materials and will continue in full force and effect until terminated in\\naccordance with the terms and conditions herein. Meta may terminate this Agreement if you are in\\nbreach of any term or condition of this Agreement. Upon termination of this Agreement, you shall delete\\nand cease use of the Llama Materials. Sections 3, 4 and 7 shall survive the termination of this\\nAgreement.\\n\\n7. Governing Law and Jurisdiction. This Agreement will be governed and construed under the laws of\\nthe State of California without regard to choice of law principles, and the UN Convention on Contracts\\nfor the International Sale of Goods does not apply to this Agreement. The courts of California shall have\\nexclusive jurisdiction of any dispute arising out of this Agreement.\\n\\n# Llama 3.1 Acceptable Use Policy\\n\\nMeta is committed to promoting safe and fair use of its tools and features, including Llama 3.1. If you\\naccess or use Llama 3.1, you agree to this Acceptable Use Policy (“Policy”). The most recent copy of\\nthis policy can be found at [https://llama.meta.com/llama3_1/use-policy](https://llama.meta.com/llama3_1/use-policy)\\n\\n## Prohibited Uses\\n\\nWe want everyone to use Llama 3.1 safely and responsibly. You agree you will not use, or allow\\nothers to use, Llama 3.1 to:\\n\\n1. Violate the law or others’ rights, including to:\\n    1. Engage in, promote, generate, contribute to, encourage, plan, incite, or further illegal or unlawful activity or content, such as:\\n        1. Violence or terrorism\\n        2. Exploitation or harm to children, including the solicitation, creation, acquisition, or dissemination of child exploitative content or failure to report Child Sexual Abuse Material\\n        3. Human trafficking, exploitation, and sexual violence\\n        4. The illegal distribution of information or materials to minors, including obscene materials, or failure to employ legally required age-gating in connection with such information or materials.\\n        5. Sexual solicitation\\n        6. Any other criminal activity\\n    3. Engage in, promote, incite, or facilitate the harassment, abuse, threatening, or bullying of individuals or groups of individuals\\n    4. Engage in, promote, incite, or facilitate discrimination or other unlawful or harmful conduct in the provision of employment, employment benefits, credit, housing, other economic benefits, or other essential goods and services\\n    5. Engage in the unauthorized or unlicensed practice of any profession including, but not limited to, financial, legal, medical/health, or related professional practices\\n    6. Collect, process, disclose, generate, or infer health, demographic, or other sensitive personal or private information about individuals without rights and consents required by applicable laws\\n    7. Engage in or facilitate any action or generate any content that infringes, misappropriates, or otherwise violates any third-party rights, including the outputs or results of any products or services using the Llama Materials\\n    8. Create, generate, or facilitate the creation of malicious code, malware, computer viruses or do anything else that could disable, overburden, interfere with or impair the proper working, integrity, operation or appearance of a website or computer system\\n\\n2. Engage in, promote, incite, facilitate, or assist in the planning or development of activities that present a risk of death or bodily harm to individuals, including use of Llama 3.1 related to the following:\\n    1. Military, warfare, nuclear industries or applications, espionage, use for materials or activities that are subject to the International Traffic Arms Regulations (ITAR) maintained by the United States Department of State\\n    2. Guns and illegal weapons (including weapon development)\\n    3. Illegal drugs and regulated/controlled substances\\n    4. Operation of critical infrastructure, transportation technologies, or heavy machinery\\n    5. Self-harm or harm to others, including suicide, cutting, and eating disorders\\n    6. Any content intended to incite or promote violence, abuse, or any infliction of bodily harm to an individual\\n\\n3. Intentionally deceive or mislead others, including use of Llama 3.1 related to the following:\\n    1. Generating, promoting, or furthering fraud or the creation or promotion of disinformation\\n    2. Generating, promoting, or furthering defamatory content, including the creation of defamatory statements, images, or other content\\n    3. Generating, promoting, or further distributing spam\\n    4. Impersonating another individual without consent, authorization, or legal right\\n    5. Representing that the use of Llama 3.1 or outputs are human-generated\\n    6. Generating or facilitating false online engagement, including fake reviews and other means of fake online engagement\\n\\n4. Fail to appropriately disclose to end users any known dangers of your AI system\\n\\nPlease report any violation of this Policy, software “bug,” or other problems that could lead to a violation\\nof this Policy through one of the following means:\\n\\n* Reporting issues with the model: [https://github.com/meta-llama/llama-models/issues](https://github.com/meta-llama/llama-models/issues)\\n* Reporting risky content generated by the model: developers.facebook.com/llama_output_feedback\\n* Reporting bugs and security concerns: facebook.com/whitehat/info\\n* Reporting violations of the Acceptable Use Policy or unlicensed uses of Llama 3.1: LlamaUseReport@meta.com\"\\n',\n",
       " 'parameters': 'stop                           \"<|start_header_id|>\"\\nstop                           \"<|end_header_id|>\"\\nstop                           \"<|eot_id|>\"',\n",
       " 'template': '{{ if .Messages }}\\n{{- if or .System .Tools }}<|start_header_id|>system<|end_header_id|>\\n{{- if .System }}\\n\\n{{ .System }}\\n{{- end }}\\n{{- if .Tools }}\\n\\nYou are a helpful assistant with tool calling capabilities. When you receive a tool call response, use the output to format an answer to the orginal use question.\\n{{- end }}\\n{{- end }}<|eot_id|>\\n{{- range $i, $_ := .Messages }}\\n{{- $last := eq (len (slice $.Messages $i)) 1 }}\\n{{- if eq .Role \"user\" }}<|start_header_id|>user<|end_header_id|>\\n{{- if and $.Tools $last }}\\n\\nGiven the following functions, please respond with a JSON for a function call with its proper arguments that best answers the given prompt.\\n\\nRespond in the format {\"name\": function name, \"parameters\": dictionary of argument name and its value}. Do not use variables.\\n\\n{{ $.Tools }}\\n{{- end }}\\n\\n{{ .Content }}<|eot_id|>{{ if $last }}<|start_header_id|>assistant<|end_header_id|>\\n\\n{{ end }}\\n{{- else if eq .Role \"assistant\" }}<|start_header_id|>assistant<|end_header_id|>\\n{{- if .ToolCalls }}\\n\\n{{- range .ToolCalls }}{\"name\": \"{{ .Function.Name }}\", \"parameters\": {{ .Function.Arguments }}}{{ end }}\\n{{- else }}\\n\\n{{ .Content }}{{ if not $last }}<|eot_id|>{{ end }}\\n{{- end }}\\n{{- else if eq .Role \"tool\" }}<|start_header_id|>ipython<|end_header_id|>\\n\\n{{ .Content }}<|eot_id|>{{ if $last }}<|start_header_id|>assistant<|end_header_id|>\\n\\n{{ end }}\\n{{- end }}\\n{{- end }}\\n{{- else }}\\n{{- if .System }}<|start_header_id|>system<|end_header_id|>\\n\\n{{ .System }}<|eot_id|>{{ end }}{{ if .Prompt }}<|start_header_id|>user<|end_header_id|>\\n\\n{{ .Prompt }}<|eot_id|>{{ end }}<|start_header_id|>assistant<|end_header_id|>\\n\\n{{ end }}{{ .Response }}{{ if .Response }}<|eot_id|>{{ end }}',\n",
       " 'details': {'parent_model': '',\n",
       "  'format': 'gguf',\n",
       "  'family': 'llama',\n",
       "  'families': ['llama'],\n",
       "  'parameter_size': '8.0B',\n",
       "  'quantization_level': 'Q4_0'},\n",
       " 'model_info': {'general.architecture': 'llama',\n",
       "  'general.basename': 'Meta-Llama-3.1',\n",
       "  'general.file_type': 2,\n",
       "  'general.finetune': 'Instruct',\n",
       "  'general.languages': ['en', 'de', 'fr', 'it', 'pt', 'hi', 'es', 'th'],\n",
       "  'general.license': 'llama3.1',\n",
       "  'general.parameter_count': 8030261312,\n",
       "  'general.quantization_version': 2,\n",
       "  'general.size_label': '8B',\n",
       "  'general.tags': ['facebook',\n",
       "   'meta',\n",
       "   'pytorch',\n",
       "   'llama',\n",
       "   'llama-3',\n",
       "   'text-generation'],\n",
       "  'general.type': 'model',\n",
       "  'llama.attention.head_count': 32,\n",
       "  'llama.attention.head_count_kv': 8,\n",
       "  'llama.attention.layer_norm_rms_epsilon': 1e-05,\n",
       "  'llama.block_count': 32,\n",
       "  'llama.context_length': 131072,\n",
       "  'llama.embedding_length': 4096,\n",
       "  'llama.feed_forward_length': 14336,\n",
       "  'llama.rope.dimension_count': 128,\n",
       "  'llama.rope.freq_base': 500000,\n",
       "  'llama.vocab_size': 128256,\n",
       "  'tokenizer.ggml.bos_token_id': 128000,\n",
       "  'tokenizer.ggml.eos_token_id': 128009,\n",
       "  'tokenizer.ggml.merges': None,\n",
       "  'tokenizer.ggml.model': 'gpt2',\n",
       "  'tokenizer.ggml.pre': 'llama-bpe',\n",
       "  'tokenizer.ggml.token_type': None,\n",
       "  'tokenizer.ggml.tokens': None},\n",
       " 'modified_at': '2024-08-08T14:13:55.590492785+08:00'}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ollama.show('llama3.1')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "bdef5c1a",
   "metadata": {},
   "source": [
    "### 创建模型\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "08c4d692",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'status': 'success'}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "modelfile='''\n",
    "FROM llama3.1\n",
    "SYSTEM 你是超级马里奥兄弟中的马里奥。\n",
    "'''\n",
    "\n",
    "ollama.create(model='example', modelfile=modelfile)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dd382053",
   "metadata": {},
   "source": [
    "### 复制模型\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "62f521ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "ollama.copy('llama3.1', 'user/llama3.1')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "614b0751",
   "metadata": {},
   "source": [
    "### 删除模型\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4461ccb8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'status': 'success'}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ollama.delete('example')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2fc44f49",
   "metadata": {},
   "source": [
    "### 拉取模型\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "981bc1aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "ollama.pull('llama3.1')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75ed2c3a",
   "metadata": {},
   "source": [
    "### 推送模型\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4cc1f37c",
   "metadata": {},
   "outputs": [],
   "source": [
    "ollama.push('user/llama3.1')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "9572b37f",
   "metadata": {},
   "source": [
    "### 生成嵌入\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "846aceba",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'embedding': [-2.141606569290161,\n",
       "  0.8904281854629517,\n",
       "  -0.8698991537094116,\n",
       "  1.1863431930541992,\n",
       "  1.266108512878418,\n",
       "  1.3763099908828735,\n",
       "  0.13048076629638672,\n",
       "  0.8475880026817322,\n",
       "  5.382612228393555,\n",
       "  -0.8182613849639893,\n",
       "  2.451780319213867,\n",
       "  -2.0489768981933594,\n",
       "  2.439929246902466,\n",
       "  1.1848701238632202,\n",
       "  -1.5042893886566162,\n",
       "  1.523100733757019,\n",
       "  3.5554730892181396,\n",
       "  2.3698158264160156,\n",
       "  1.5025027990341187,\n",
       "  0.3790349066257477,\n",
       "  -0.9003847241401672,\n",
       "  0.3219177722930908,\n",
       "  -1.655588150024414,\n",
       "  1.245728850364685,\n",
       "  -0.5998708605766296,\n",
       "  -0.21839220821857452,\n",
       "  0.31109553575515747,\n",
       "  -0.3510762155056,\n",
       "  -2.165977716445923,\n",
       "  -0.48920106887817383,\n",
       "  -1.583941102027893,\n",
       "  -1.799527645111084,\n",
       "  -0.9458537101745605,\n",
       "  1.1263484954833984,\n",
       "  0.12802058458328247,\n",
       "  -0.32982853055000305,\n",
       "  0.596611499786377,\n",
       "  -1.6250736713409424,\n",
       "  -0.36680158972740173,\n",
       "  -3.769871473312378,\n",
       "  0.6333229541778564,\n",
       "  -1.073930263519287,\n",
       "  -1.5813485383987427,\n",
       "  1.8329622745513916,\n",
       "  2.2371490001678467,\n",
       "  1.8337916135787964,\n",
       "  0.6859056949615479,\n",
       "  1.5068888664245605,\n",
       "  1.372693419456482,\n",
       "  -2.7701125144958496,\n",
       "  0.8734321594238281,\n",
       "  -1.172940731048584,\n",
       "  0.9352511763572693,\n",
       "  -0.05263785272836685,\n",
       "  0.8902302384376526,\n",
       "  -0.31441599130630493,\n",
       "  -0.21530118584632874,\n",
       "  0.6120880246162415,\n",
       "  -1.3579423427581787,\n",
       "  -1.6052987575531006,\n",
       "  -2.5759379863739014,\n",
       "  -1.1635053157806396,\n",
       "  0.29505667090415955,\n",
       "  -1.3268837928771973,\n",
       "  1.9097554683685303,\n",
       "  -0.6262404918670654,\n",
       "  -1.136494755744934,\n",
       "  1.8535956144332886,\n",
       "  1.5468777418136597,\n",
       "  0.4984789490699768,\n",
       "  2.0766866207122803,\n",
       "  2.4356849193573,\n",
       "  0.19224058091640472,\n",
       "  0.419318288564682,\n",
       "  1.1154251098632812,\n",
       "  -2.2855911254882812,\n",
       "  -1.0821539163589478,\n",
       "  1.0694432258605957,\n",
       "  0.5782574415206909,\n",
       "  -1.9651083946228027,\n",
       "  -3.200762987136841,\n",
       "  0.009428230114281178,\n",
       "  -1.3236075639724731,\n",
       "  2.4419147968292236,\n",
       "  0.6759130954742432,\n",
       "  -1.4580869674682617,\n",
       "  -0.20548439025878906,\n",
       "  0.28553080558776855,\n",
       "  -1.4538254737854004,\n",
       "  -0.034732867032289505,\n",
       "  0.6720460057258606,\n",
       "  0.6265965104103088,\n",
       "  1.2221297025680542,\n",
       "  0.9535624384880066,\n",
       "  -2.655444860458374,\n",
       "  -0.2711804509162903,\n",
       "  0.2985227406024933,\n",
       "  1.6304196119308472,\n",
       "  -1.2137668132781982,\n",
       "  -2.760436773300171,\n",
       "  0.04087873548269272,\n",
       "  0.434949666261673,\n",
       "  2.159113883972168,\n",
       "  -0.8327275514602661,\n",
       "  -2.937838077545166,\n",
       "  3.468491554260254,\n",
       "  -0.32694005966186523,\n",
       "  -0.505435049533844,\n",
       "  -3.882540464401245,\n",
       "  -0.6058856844902039,\n",
       "  1.9717130661010742,\n",
       "  0.3483884036540985,\n",
       "  -0.4296397566795349,\n",
       "  1.1912249326705933,\n",
       "  0.09629687666893005,\n",
       "  -4.099006652832031,\n",
       "  -2.569674253463745,\n",
       "  1.6126357316970825,\n",
       "  -0.7950485944747925,\n",
       "  -0.17055894434452057,\n",
       "  0.5328317880630493,\n",
       "  0.41240134835243225,\n",
       "  0.5338537693023682,\n",
       "  -1.1675225496292114,\n",
       "  0.40439465641975403,\n",
       "  0.3132927417755127,\n",
       "  1.352519154548645,\n",
       "  2.9268059730529785,\n",
       "  4.225004196166992,\n",
       "  -0.7514230012893677,\n",
       "  -2.2337865829467773,\n",
       "  3.0753495693206787,\n",
       "  0.9071045517921448,\n",
       "  1.144636869430542,\n",
       "  -1.285773515701294,\n",
       "  -1.302869439125061,\n",
       "  4.966525554656982,\n",
       "  0.016617150977253914,\n",
       "  -1.7328401803970337,\n",
       "  -1.8693846464157104,\n",
       "  0.8261747360229492,\n",
       "  -0.567732036113739,\n",
       "  0.49551907181739807,\n",
       "  -0.7679855823516846,\n",
       "  0.3701934814453125,\n",
       "  1.4102826118469238,\n",
       "  -0.5568163990974426,\n",
       "  -0.09602304548025131,\n",
       "  1.6075682640075684,\n",
       "  0.9501043558120728,\n",
       "  1.4359067678451538,\n",
       "  -1.5365252494812012,\n",
       "  -3.1339707374572754,\n",
       "  -2.2457187175750732,\n",
       "  1.331634521484375,\n",
       "  0.7147477865219116,\n",
       "  -1.517996072769165,\n",
       "  0.7572647333145142,\n",
       "  1.586628794670105,\n",
       "  0.26660969853401184,\n",
       "  -1.7781751155853271,\n",
       "  -0.8518422245979309,\n",
       "  -0.2277132272720337,\n",
       "  1.498913049697876,\n",
       "  -1.1132246255874634,\n",
       "  -0.4517251253128052,\n",
       "  0.3667278289794922,\n",
       "  -0.050693728029727936,\n",
       "  -1.6460888385772705,\n",
       "  -0.10755433142185211,\n",
       "  2.1459152698516846,\n",
       "  2.835477828979492,\n",
       "  1.1457676887512207,\n",
       "  -1.4717991352081299,\n",
       "  -0.16585196554660797,\n",
       "  -2.7628862857818604,\n",
       "  -0.10206510871648788,\n",
       "  1.3758662939071655,\n",
       "  0.624443531036377,\n",
       "  0.7833728194236755,\n",
       "  -1.6225701570510864,\n",
       "  -1.3548336029052734,\n",
       "  -4.6615400314331055,\n",
       "  0.2039586454629898,\n",
       "  -4.009902000427246,\n",
       "  -2.4371275901794434,\n",
       "  -2.6332030296325684,\n",
       "  -0.34081366658210754,\n",
       "  -1.0472350120544434,\n",
       "  1.3321352005004883,\n",
       "  -0.6262301802635193,\n",
       "  0.4983615279197693,\n",
       "  -0.39990049600601196,\n",
       "  1.257179617881775,\n",
       "  -1.442838430404663,\n",
       "  0.25781184434890747,\n",
       "  0.5039374828338623,\n",
       "  1.0563440322875977,\n",
       "  0.3044097125530243,\n",
       "  -1.662195086479187,\n",
       "  0.7668150663375854,\n",
       "  -4.442120552062988,\n",
       "  0.05787011981010437,\n",
       "  -1.5176244974136353,\n",
       "  2.5530717372894287,\n",
       "  -2.1473441123962402,\n",
       "  -1.060994267463684,\n",
       "  -1.627281904220581,\n",
       "  2.3737828731536865,\n",
       "  -2.596670389175415,\n",
       "  3.1945443153381348,\n",
       "  -1.5225582122802734,\n",
       "  0.11675112694501877,\n",
       "  1.1833195686340332,\n",
       "  3.753342866897583,\n",
       "  1.9751228094100952,\n",
       "  1.528395652770996,\n",
       "  -0.2097066193819046,\n",
       "  -0.08923263102769852,\n",
       "  1.4716628789901733,\n",
       "  0.2908525764942169,\n",
       "  -0.8206835389137268,\n",
       "  -3.6607420444488525,\n",
       "  -0.6214147806167603,\n",
       "  2.6197943687438965,\n",
       "  2.086848258972168,\n",
       "  -0.5420745015144348,\n",
       "  -4.02748966217041,\n",
       "  -0.8803300857543945,\n",
       "  -0.7072991132736206,\n",
       "  0.14433428645133972,\n",
       "  -0.5907505750656128,\n",
       "  1.7281126976013184,\n",
       "  0.17129044234752655,\n",
       "  -1.865834355354309,\n",
       "  -0.3285330832004547,\n",
       "  1.2586458921432495,\n",
       "  0.6487223505973816,\n",
       "  3.6322734355926514,\n",
       "  -1.6585928201675415,\n",
       "  0.8487664461135864,\n",
       "  0.6920725107192993,\n",
       "  0.4334917366504669,\n",
       "  3.1248393058776855,\n",
       "  2.7871060371398926,\n",
       "  -0.4588187634944916,\n",
       "  -2.592475175857544,\n",
       "  1.469657063484192,\n",
       "  -1.2655566930770874,\n",
       "  -1.7979121208190918,\n",
       "  -1.1743757724761963,\n",
       "  1.219067096710205,\n",
       "  -0.3587149381637573,\n",
       "  0.10623838752508163,\n",
       "  0.4712030291557312,\n",
       "  0.6139305233955383,\n",
       "  -0.4113064110279083,\n",
       "  -0.9073101282119751,\n",
       "  1.4800314903259277,\n",
       "  5.032802581787109,\n",
       "  2.80112361907959,\n",
       "  0.8278696537017822,\n",
       "  -0.7236995697021484,\n",
       "  0.40392836928367615,\n",
       "  -1.0051634311676025,\n",
       "  -1.7367056608200073,\n",
       "  -0.21743907034397125,\n",
       "  0.24987003207206726,\n",
       "  2.193871259689331,\n",
       "  -1.1350626945495605,\n",
       "  0.19672609865665436,\n",
       "  2.9022345542907715,\n",
       "  -1.8395036458969116,\n",
       "  -2.197387456893921,\n",
       "  -1.3794361352920532,\n",
       "  0.03779064118862152,\n",
       "  1.219356656074524,\n",
       "  -3.0498392581939697,\n",
       "  -0.30512499809265137,\n",
       "  0.5016427636146545,\n",
       "  0.41571611166000366,\n",
       "  1.0073341131210327,\n",
       "  0.5929012894630432,\n",
       "  -0.9989046454429626,\n",
       "  0.1639309823513031,\n",
       "  -1.86729097366333,\n",
       "  -1.0539989471435547,\n",
       "  -0.5295321941375732,\n",
       "  0.8427442312240601,\n",
       "  -14.737140655517578,\n",
       "  4.6059417724609375,\n",
       "  -0.6416168808937073,\n",
       "  0.6718973517417908,\n",
       "  -1.4040911197662354,\n",
       "  -0.6222323775291443,\n",
       "  -2.4661734104156494,\n",
       "  -1.0667577981948853,\n",
       "  0.4594738483428955,\n",
       "  -0.794922947883606,\n",
       "  -3.5665102005004883,\n",
       "  -0.5596388578414917,\n",
       "  -0.7181586623191833,\n",
       "  -1.7875752449035645,\n",
       "  -0.1309313029050827,\n",
       "  -0.9546880722045898,\n",
       "  -0.5078127384185791,\n",
       "  2.388559579849243,\n",
       "  3.0209081172943115,\n",
       "  -1.8021259307861328,\n",
       "  0.2711820900440216,\n",
       "  -2.9033608436584473,\n",
       "  4.104788780212402,\n",
       "  -0.706497311592102,\n",
       "  0.9824011921882629,\n",
       "  0.6978272795677185,\n",
       "  0.25776243209838867,\n",
       "  -1.75860595703125,\n",
       "  -0.31918954849243164,\n",
       "  0.6616101861000061,\n",
       "  -2.0943102836608887,\n",
       "  -17.537803649902344,\n",
       "  -1.466488242149353,\n",
       "  -0.6677706837654114,\n",
       "  -0.9122726321220398,\n",
       "  -1.049795389175415,\n",
       "  -0.5159823894500732,\n",
       "  0.9040600657463074,\n",
       "  -1.3651551008224487,\n",
       "  -0.5878385901451111,\n",
       "  1.2577775716781616,\n",
       "  -0.5365136861801147,\n",
       "  -2.6891393661499023,\n",
       "  -2.514183759689331,\n",
       "  -1.0114997625350952,\n",
       "  -0.07814479619264603,\n",
       "  1.3139734268188477,\n",
       "  -2.4078712463378906,\n",
       "  -0.5880059003829956,\n",
       "  -0.19319453835487366,\n",
       "  0.8400030136108398,\n",
       "  1.899041771888733,\n",
       "  0.1707555651664734,\n",
       "  -2.0590760707855225,\n",
       "  0.6900654435157776,\n",
       "  1.2607402801513672,\n",
       "  -1.657417893409729,\n",
       "  -1.1533153057098389,\n",
       "  0.015925530344247818,\n",
       "  -1.7630765438079834,\n",
       "  1.0959316492080688,\n",
       "  0.4380185008049011,\n",
       "  2.9297406673431396,\n",
       "  -1.634315013885498,\n",
       "  0.03904478996992111,\n",
       "  0.5922812819480896,\n",
       "  1.7611616849899292,\n",
       "  0.9313859343528748,\n",
       "  -2.017979860305786,\n",
       "  1.0220855474472046,\n",
       "  0.6351048946380615,\n",
       "  0.31172043085098267,\n",
       "  0.4835313558578491,\n",
       "  -2.2645297050476074,\n",
       "  0.7291397452354431,\n",
       "  -1.2035608291625977,\n",
       "  0.8821070790290833,\n",
       "  -1.2970576286315918,\n",
       "  2.9256458282470703,\n",
       "  -0.2763405740261078,\n",
       "  -0.5954005122184753,\n",
       "  0.35969868302345276,\n",
       "  0.41047853231430054,\n",
       "  2.0453402996063232,\n",
       "  -0.20811906456947327,\n",
       "  1.8403003215789795,\n",
       "  1.3956823348999023,\n",
       "  2.289313793182373,\n",
       "  1.4270713329315186,\n",
       "  2.0248382091522217,\n",
       "  -1.592235803604126,\n",
       "  2.120612144470215,\n",
       "  -1.3011518716812134,\n",
       "  -1.1042388677597046,\n",
       "  -0.23838728666305542,\n",
       "  1.9239490032196045,\n",
       "  0.819405198097229,\n",
       "  0.3180001974105835,\n",
       "  2.789867877960205,\n",
       "  -1.2296315431594849,\n",
       "  -1.286137580871582,\n",
       "  0.378562867641449,\n",
       "  1.630525827407837,\n",
       "  0.14203783869743347,\n",
       "  4.488844394683838,\n",
       "  0.7897916436195374,\n",
       "  4.0298919677734375,\n",
       "  -0.5971526503562927,\n",
       "  -1.0980937480926514,\n",
       "  -3.5724637508392334,\n",
       "  1.1487926244735718,\n",
       "  -1.5467742681503296,\n",
       "  1.2888778448104858,\n",
       "  -6.954930782318115,\n",
       "  -2.158686876296997,\n",
       "  -0.2585836946964264,\n",
       "  0.47156205773353577,\n",
       "  1.9749279022216797,\n",
       "  0.5835241079330444,\n",
       "  1.9955517053604126,\n",
       "  -0.021793413907289505,\n",
       "  1.1273361444473267,\n",
       "  0.40415695309638977,\n",
       "  3.4191226959228516,\n",
       "  -1.0401678085327148,\n",
       "  -1.5004457235336304,\n",
       "  -0.60793536901474,\n",
       "  -1.15792977809906,\n",
       "  0.5052903890609741,\n",
       "  1.0831584930419922,\n",
       "  -1.7182252407073975,\n",
       "  -0.7593585848808289,\n",
       "  0.05135611817240715,\n",
       "  2.8598055839538574,\n",
       "  -2.1067311763763428,\n",
       "  0.3220198452472687,\n",
       "  1.9500305652618408,\n",
       "  -0.7710726261138916,\n",
       "  -1.181457281112671,\n",
       "  -0.38566410541534424,\n",
       "  -2.1113436222076416,\n",
       "  1.7805174589157104,\n",
       "  -1.1916851997375488,\n",
       "  1.636562466621399,\n",
       "  -4.0781426429748535,\n",
       "  1.1313115358352661,\n",
       "  -2.0501296520233154,\n",
       "  -1.8857041597366333,\n",
       "  0.7284771203994751,\n",
       "  -1.575326681137085,\n",
       "  -0.14418300986289978,\n",
       "  -1.9089128971099854,\n",
       "  -2.1525776386260986,\n",
       "  0.059381064027547836,\n",
       "  -1.3577625751495361,\n",
       "  -1.8320611715316772,\n",
       "  2.0796022415161133,\n",
       "  0.46398013830184937,\n",
       "  -0.19983558356761932,\n",
       "  1.9563031196594238,\n",
       "  1.3514219522476196,\n",
       "  -1.4308035373687744,\n",
       "  -1.4336507320404053,\n",
       "  2.2226154804229736,\n",
       "  0.5494446158409119,\n",
       "  -1.2228267192840576,\n",
       "  -2.7071733474731445,\n",
       "  -2.3859636783599854,\n",
       "  1.6645859479904175,\n",
       "  -3.0104057788848877,\n",
       "  -0.23305568099021912,\n",
       "  -2.0595040321350098,\n",
       "  2.032095432281494,\n",
       "  0.3087885081768036,\n",
       "  2.7015247344970703,\n",
       "  2.2812235355377197,\n",
       "  -1.0732049942016602,\n",
       "  -0.10599172115325928,\n",
       "  0.45011305809020996,\n",
       "  -2.2258386611938477,\n",
       "  -0.8584228754043579,\n",
       "  0.9014346599578857,\n",
       "  -2.943877935409546,\n",
       "  1.126203179359436,\n",
       "  -2.600191831588745,\n",
       "  -1.0183652639389038,\n",
       "  -2.2298688888549805,\n",
       "  -3.2960448265075684,\n",
       "  -2.415147542953491,\n",
       "  -3.290217399597168,\n",
       "  -0.004704334307461977,\n",
       "  2.153970956802368,\n",
       "  -0.29592365026474,\n",
       "  -0.39617228507995605,\n",
       "  1.261729121208191,\n",
       "  1.1924985647201538,\n",
       "  0.3358314335346222,\n",
       "  0.45477721095085144,\n",
       "  -1.6346794366836548,\n",
       "  -2.3480801582336426,\n",
       "  -4.4086456298828125,\n",
       "  1.8347350358963013,\n",
       "  0.008978004567325115,\n",
       "  -0.8333563208580017,\n",
       "  -1.0928568840026855,\n",
       "  -0.19925516843795776,\n",
       "  0.6623928546905518,\n",
       "  -0.6387724280357361,\n",
       "  1.5352511405944824,\n",
       "  0.29127734899520874,\n",
       "  -1.0339763164520264,\n",
       "  0.24053144454956055,\n",
       "  -0.6139026880264282,\n",
       "  -1.6250054836273193,\n",
       "  0.9083845019340515,\n",
       "  3.5885791778564453,\n",
       "  0.8736315369606018,\n",
       "  -2.436521291732788,\n",
       "  2.857588052749634,\n",
       "  -1.9642791748046875,\n",
       "  -0.06156836822628975,\n",
       "  1.1673961877822876,\n",
       "  1.7342077493667603,\n",
       "  -0.9740309715270996,\n",
       "  0.1045224666595459,\n",
       "  -1.473490834236145,\n",
       "  2.3328378200531006,\n",
       "  -0.134441077709198,\n",
       "  -1.6622973680496216,\n",
       "  -12.929512977600098,\n",
       "  0.7648341059684753,\n",
       "  -3.617607831954956,\n",
       "  -0.6748345494270325,\n",
       "  0.24482950568199158,\n",
       "  4.266307353973389,\n",
       "  0.33710071444511414,\n",
       "  -0.3127603232860565,\n",
       "  -1.2119778394699097,\n",
       "  -0.6898422241210938,\n",
       "  0.5904563069343567,\n",
       "  3.7787249088287354,\n",
       "  -2.6684353351593018,\n",
       "  0.40933752059936523,\n",
       "  -1.420748233795166,\n",
       "  -2.7541370391845703,\n",
       "  0.4831636846065521,\n",
       "  -0.5423809885978699,\n",
       "  1.0705817937850952,\n",
       "  0.8990297913551331,\n",
       "  1.1806398630142212,\n",
       "  0.9041151404380798,\n",
       "  0.32857149839401245,\n",
       "  2.2551064491271973,\n",
       "  0.15169043838977814,\n",
       "  -1.0303529500961304,\n",
       "  -0.10465388745069504,\n",
       "  -0.2756335735321045,\n",
       "  0.7998781204223633,\n",
       "  1.2618263959884644,\n",
       "  0.3876189589500427,\n",
       "  -0.7004474401473999,\n",
       "  -1.899153470993042,\n",
       "  1.5821467638015747,\n",
       "  1.4929070472717285,\n",
       "  -0.647159218788147,\n",
       "  -0.31766772270202637,\n",
       "  3.470689535140991,\n",
       "  0.12765438854694366,\n",
       "  -0.8335879445075989,\n",
       "  -1.4630321264266968,\n",
       "  3.2698071002960205,\n",
       "  0.817081093788147,\n",
       "  1.598492980003357,\n",
       "  1.8642204999923706,\n",
       "  5.00797176361084,\n",
       "  0.588482677936554,\n",
       "  -1.063058614730835,\n",
       "  -2.4351654052734375,\n",
       "  -0.9891263246536255,\n",
       "  0.6763582229614258,\n",
       "  -0.35050031542778015,\n",
       "  3.6935887336730957,\n",
       "  1.5129681825637817,\n",
       "  -2.556837320327759,\n",
       "  2.3232219219207764,\n",
       "  1.8705880641937256,\n",
       "  -1.3625518083572388,\n",
       "  -2.351425886154175,\n",
       "  0.6810010671615601,\n",
       "  -0.1496649831533432,\n",
       "  0.29774537682533264,\n",
       "  -1.6260193586349487,\n",
       "  -2.0876755714416504,\n",
       "  -4.9023756980896,\n",
       "  1.8834842443466187,\n",
       "  -0.478559672832489,\n",
       "  1.369156837463379,\n",
       "  1.9408516883850098,\n",
       "  1.1903884410858154,\n",
       "  -2.17952561378479,\n",
       "  -1.509120225906372,\n",
       "  -1.4082822799682617,\n",
       "  0.43398797512054443,\n",
       "  -1.378043293952942,\n",
       "  5.895392417907715,\n",
       "  -2.8077330589294434,\n",
       "  1.1925143003463745,\n",
       "  2.693089246749878,\n",
       "  -0.39846375584602356,\n",
       "  0.6172456741333008,\n",
       "  -2.529987335205078,\n",
       "  2.1135501861572266,\n",
       "  0.09608251601457596,\n",
       "  -1.8599995374679565,\n",
       "  0.7448298335075378,\n",
       "  -0.3685529828071594,\n",
       "  -1.4061098098754883,\n",
       "  -0.2378014773130417,\n",
       "  -0.9288275837898254,\n",
       "  -0.09570521116256714,\n",
       "  0.1733826845884323,\n",
       "  6.049293041229248,\n",
       "  -0.19758641719818115,\n",
       "  -1.7532851696014404,\n",
       "  -2.272775173187256,\n",
       "  0.308845192193985,\n",
       "  -4.664058685302734,\n",
       "  -0.49628588557243347,\n",
       "  1.0224632024765015,\n",
       "  1.4408345222473145,\n",
       "  0.8545000553131104,\n",
       "  1.396897554397583,\n",
       "  0.5195348858833313,\n",
       "  2.251084566116333,\n",
       "  -0.6015908718109131,\n",
       "  0.8192762732505798,\n",
       "  0.6290644407272339,\n",
       "  -2.7853200435638428,\n",
       "  0.2339564859867096,\n",
       "  3.1974124908447266,\n",
       "  -2.4338431358337402,\n",
       "  0.14289848506450653,\n",
       "  3.7894558906555176,\n",
       "  2.5301661491394043,\n",
       "  -0.29850128293037415,\n",
       "  1.6355857849121094,\n",
       "  0.49170970916748047,\n",
       "  0.7823848128318787,\n",
       "  -2.524460554122925,\n",
       "  -0.8889055252075195,\n",
       "  0.8872153759002686,\n",
       "  0.15560069680213928,\n",
       "  -3.8746747970581055,\n",
       "  1.0053468942642212,\n",
       "  -0.008933373726904392,\n",
       "  -1.1944204568862915,\n",
       "  -0.7126156687736511,\n",
       "  1.454803705215454,\n",
       "  -0.34532925486564636,\n",
       "  4.171834468841553,\n",
       "  1.546078085899353,\n",
       "  -0.9918440580368042,\n",
       "  0.1732136756181717,\n",
       "  1.2240753173828125,\n",
       "  5.178974151611328,\n",
       "  -0.4586924612522125,\n",
       "  1.1496598720550537,\n",
       "  -1.0970996618270874,\n",
       "  6.445868492126465,\n",
       "  -0.044611092656850815,\n",
       "  -0.49013009667396545,\n",
       "  -2.659512996673584,\n",
       "  -4.659774303436279,\n",
       "  -0.6234380602836609,\n",
       "  -1.1961437463760376,\n",
       "  -2.039496660232544,\n",
       "  -0.6501039266586304,\n",
       "  -2.374300479888916,\n",
       "  0.22174358367919922,\n",
       "  -1.3473100662231445,\n",
       "  1.0068535804748535,\n",
       "  4.57716703414917,\n",
       "  -0.2319771945476532,\n",
       "  2.1337502002716064,\n",
       "  -2.0592849254608154,\n",
       "  2.1169493198394775,\n",
       "  2.5774917602539062,\n",
       "  -0.7879356145858765,\n",
       "  -2.91414213180542,\n",
       "  -0.6388831734657288,\n",
       "  -1.8645533323287964,\n",
       "  -1.9414887428283691,\n",
       "  -0.5510943531990051,\n",
       "  0.36079326272010803,\n",
       "  0.28968116641044617,\n",
       "  0.4521782696247101,\n",
       "  -1.2282814979553223,\n",
       "  0.9103152751922607,\n",
       "  0.1591401994228363,\n",
       "  3.2087502479553223,\n",
       "  0.5217716693878174,\n",
       "  -1.1911859512329102,\n",
       "  1.3493313789367676,\n",
       "  1.9818717241287231,\n",
       "  -0.32698291540145874,\n",
       "  1.1787387132644653,\n",
       "  0.8297609686851501,\n",
       "  1.2022547721862793,\n",
       "  -0.9696376323699951,\n",
       "  -1.409176230430603,\n",
       "  0.3691341280937195,\n",
       "  0.8817304372787476,\n",
       "  0.6895384192466736,\n",
       "  1.0081735849380493,\n",
       "  1.9448390007019043,\n",
       "  -1.3058310747146606,\n",
       "  2.519040822982788,\n",
       "  2.1895575523376465,\n",
       "  -1.343166708946228,\n",
       "  1.2001782655715942,\n",
       "  -3.8700411319732666,\n",
       "  -0.8454896807670593,\n",
       "  -0.6761232614517212,\n",
       "  4.501762866973877,\n",
       "  -0.1257636398077011,\n",
       "  -0.05445947125554085,\n",
       "  -1.5378835201263428,\n",
       "  -1.2772259712219238,\n",
       "  -2.3978395462036133,\n",
       "  -0.0049721114337444305,\n",
       "  -1.1318457126617432,\n",
       "  2.4105865955352783,\n",
       "  0.3273298144340515,\n",
       "  -2.3332343101501465,\n",
       "  0.6194825172424316,\n",
       "  1.9335252046585083,\n",
       "  -2.3488852977752686,\n",
       "  -0.7558260560035706,\n",
       "  -1.5434602499008179,\n",
       "  3.4175117015838623,\n",
       "  0.33960455656051636,\n",
       "  1.2951678037643433,\n",
       "  0.5776858925819397,\n",
       "  -1.2342920303344727,\n",
       "  -3.740236759185791,\n",
       "  -2.084357976913452,\n",
       "  0.3946254849433899,\n",
       "  -1.4335511922836304,\n",
       "  1.0533697605133057,\n",
       "  2.6123428344726562,\n",
       "  1.3988432884216309,\n",
       "  -3.120814800262451,\n",
       "  1.4780129194259644,\n",
       "  0.003983605653047562,\n",
       "  0.01360942143946886,\n",
       "  1.8646841049194336,\n",
       "  0.6123495101928711,\n",
       "  -0.6678234934806824,\n",
       "  0.7114754319190979,\n",
       "  1.439009189605713,\n",
       "  -0.6897310614585876,\n",
       "  0.652370035648346,\n",
       "  1.3168525695800781,\n",
       "  0.3244667649269104,\n",
       "  0.17746250331401825,\n",
       "  1.3064032793045044,\n",
       "  -0.38542768359184265,\n",
       "  0.1679908037185669,\n",
       "  -2.4768497943878174,\n",
       "  -0.6893572807312012,\n",
       "  0.85358726978302,\n",
       "  -0.32207638025283813,\n",
       "  -0.3490943908691406,\n",
       "  3.4791438579559326,\n",
       "  0.33075252175331116,\n",
       "  -0.5532862544059753,\n",
       "  -3.3875179290771484,\n",
       "  1.4098517894744873,\n",
       "  1.9680713415145874,\n",
       "  1.38558828830719,\n",
       "  2.8709115982055664,\n",
       "  0.3878621459007263,\n",
       "  0.5282487273216248,\n",
       "  -0.9098327159881592,\n",
       "  -2.1121466159820557,\n",
       "  0.3714258372783661,\n",
       "  -0.4922318160533905,\n",
       "  -0.03247145563364029,\n",
       "  -0.027906205505132675,\n",
       "  1.190615177154541,\n",
       "  4.235495567321777,\n",
       "  1.0987913608551025,\n",
       "  -0.42817285656929016,\n",
       "  -13.950841903686523,\n",
       "  0.5811338424682617,\n",
       "  -0.126478374004364,\n",
       "  2.1842315196990967,\n",
       "  -0.4119720757007599,\n",
       "  0.671132504940033,\n",
       "  -0.7259098887443542,\n",
       "  0.5235549807548523,\n",
       "  -1.0422993898391724,\n",
       "  -2.085364580154419,\n",
       "  0.602696418762207,\n",
       "  1.481419563293457,\n",
       "  -1.502211332321167,\n",
       "  1.8994797468185425,\n",
       "  -1.4963220357894897,\n",
       "  -0.7381706833839417,\n",
       "  0.9052590727806091,\n",
       "  -1.1796711683273315,\n",
       "  -1.9797134399414062,\n",
       "  3.5052547454833984,\n",
       "  -0.4716929495334625,\n",
       "  -1.8700802326202393,\n",
       "  0.23247666656970978,\n",
       "  -1.6802481412887573,\n",
       "  -0.715488612651825,\n",
       "  0.233711376786232,\n",
       "  1.3435941934585571,\n",
       "  0.7531721591949463,\n",
       "  0.7081279754638672,\n",
       "  0.9891942143440247,\n",
       "  -1.5173914432525635,\n",
       "  -0.7612431049346924,\n",
       "  2.4279277324676514,\n",
       "  -1.3078715801239014,\n",
       "  -2.0557901859283447,\n",
       "  1.7934786081314087,\n",
       "  1.4574189186096191,\n",
       "  -1.1985719203948975,\n",
       "  -2.2674264907836914,\n",
       "  -1.486573576927185,\n",
       "  0.918102502822876,\n",
       "  -1.4608858823776245,\n",
       "  -0.8265539407730103,\n",
       "  -2.0295913219451904,\n",
       "  1.5682871341705322,\n",
       "  -0.3824594020843506,\n",
       "  0.7115692496299744,\n",
       "  0.9116715788841248,\n",
       "  -1.4215278625488281,\n",
       "  -1.3624624013900757,\n",
       "  -0.347871869802475,\n",
       "  0.9335888028144836,\n",
       "  2.1071979999542236,\n",
       "  1.366119146347046,\n",
       "  0.07252725213766098,\n",
       "  2.3184854984283447,\n",
       "  3.260589599609375,\n",
       "  1.0769022703170776,\n",
       "  -0.2414892613887787,\n",
       "  -1.2892402410507202,\n",
       "  -1.435110330581665,\n",
       "  0.8091645836830139,\n",
       "  -1.4155503511428833,\n",
       "  0.8739033341407776,\n",
       "  -1.215162754058838,\n",
       "  -0.4627421796321869,\n",
       "  0.9535046815872192,\n",
       "  0.046804893761873245,\n",
       "  0.5666629076004028,\n",
       "  0.7337482571601868,\n",
       "  -1.7161674499511719,\n",
       "  1.712760090827942,\n",
       "  -0.290303111076355,\n",
       "  1.219604253768921,\n",
       "  0.6904565095901489,\n",
       "  1.103615641593933,\n",
       "  1.896837592124939,\n",
       "  -0.7644801139831543,\n",
       "  -1.8258177042007446,\n",
       "  1.6942386627197266,\n",
       "  -0.7034926414489746,\n",
       "  0.5132864713668823,\n",
       "  -1.4160672426223755,\n",
       "  -0.4136188328266144,\n",
       "  2.9691431522369385,\n",
       "  -1.2070437669754028,\n",
       "  -0.3076217472553253,\n",
       "  -1.9516948461532593,\n",
       "  -2.9753308296203613,\n",
       "  -1.1785353422164917,\n",
       "  -2.364762544631958,\n",
       "  2.386080741882324,\n",
       "  0.08486847579479218,\n",
       "  3.152902364730835,\n",
       "  -0.23056739568710327,\n",
       "  -1.2625401020050049,\n",
       "  3.5695741176605225,\n",
       "  1.204269289970398,\n",
       "  -0.03912051022052765,\n",
       "  1.8243842124938965,\n",
       "  -0.37589430809020996,\n",
       "  -0.48757097125053406,\n",
       "  -1.7844245433807373,\n",
       "  2.0725319385528564,\n",
       "  -0.13871078193187714,\n",
       "  2.017758369445801,\n",
       "  -0.3284912109375,\n",
       "  -0.5203827023506165,\n",
       "  -1.8896540403366089,\n",
       "  2.951122999191284,\n",
       "  0.09398868680000305,\n",
       "  0.29167431592941284,\n",
       "  -0.5557024478912354,\n",
       "  -0.042683426290750504,\n",
       "  0.9334898591041565,\n",
       "  0.9805219769477844,\n",
       "  1.6339151859283447,\n",
       "  0.3452989459037781,\n",
       "  -1.5840164422988892,\n",
       "  3.848855495452881,\n",
       "  0.6507293581962585,\n",
       "  1.1967997550964355,\n",
       "  -1.0543426275253296,\n",
       "  0.5259070992469788,\n",
       "  -0.8632457852363586,\n",
       "  0.856621265411377,\n",
       "  0.32736584544181824,\n",
       "  -2.3965647220611572,\n",
       "  10.816645622253418,\n",
       "  -3.0911648273468018,\n",
       "  -0.6599356532096863,\n",
       "  0.13429903984069824,\n",
       "  -3.1507160663604736,\n",
       "  -0.6336962580680847,\n",
       "  0.5297281742095947,\n",
       "  -2.731083869934082,\n",
       "  -0.4917196035385132,\n",
       "  0.2129841148853302,\n",
       "  1.9374663829803467,\n",
       "  0.286226361989975,\n",
       "  0.7598880529403687,\n",
       "  -1.764449954032898,\n",
       "  0.14317266643047333,\n",
       "  -0.9679154753684998,\n",
       "  -4.0259013175964355,\n",
       "  2.114525318145752,\n",
       "  -1.9260096549987793,\n",
       "  3.0101656913757324,\n",
       "  1.055629849433899,\n",
       "  -0.31876885890960693,\n",
       "  -2.7966384887695312,\n",
       "  -1.1219218969345093,\n",
       "  -0.7268600463867188,\n",
       "  1.9169714450836182,\n",
       "  -1.2924209833145142,\n",
       "  0.58534175157547,\n",
       "  -0.5404273867607117,\n",
       "  -5.765933513641357,\n",
       "  1.3874876499176025,\n",
       "  -0.6057855486869812,\n",
       "  -0.19043175876140594,\n",
       "  4.133814811706543,\n",
       "  1.1681513786315918,\n",
       "  -0.07827213406562805,\n",
       "  1.4667528867721558,\n",
       "  0.8959412574768066,\n",
       "  -2.312830924987793,\n",
       "  -1.168089747428894,\n",
       "  1.3571856021881104,\n",
       "  0.42825847864151,\n",
       "  1.234194278717041,\n",
       "  -0.6151146292686462,\n",
       "  0.7035560607910156,\n",
       "  3.9028141498565674,\n",
       "  2.7364258766174316,\n",
       "  0.35277092456817627,\n",
       "  -1.7246713638305664,\n",
       "  -3.72782564163208,\n",
       "  -1.0260423421859741,\n",
       "  -0.41174373030662537,\n",
       "  -1.556456446647644,\n",
       "  3.0771660804748535,\n",
       "  0.09001152217388153,\n",
       "  -2.4649534225463867,\n",
       "  -1.5963603258132935,\n",
       "  2.472140073776245,\n",
       "  -0.4026077389717102,\n",
       "  1.1987040042877197,\n",
       "  2.9236209392547607,\n",
       "  -1.1397850513458252,\n",
       "  0.5399981141090393,\n",
       "  0.9567602276802063,\n",
       "  -1.036094307899475,\n",
       "  -3.198561191558838,\n",
       "  -0.1165996640920639,\n",
       "  -4.323287487030029,\n",
       "  1.6679149866104126,\n",
       "  1.1166578531265259,\n",
       "  0.7767750024795532,\n",
       "  -1.3931751251220703,\n",
       "  -1.845374584197998,\n",
       "  -0.6670653820037842,\n",
       "  1.1302361488342285,\n",
       "  -0.8976494073867798,\n",
       "  0.8427703380584717,\n",
       "  -0.8242907524108887,\n",
       "  1.1016132831573486,\n",
       "  3.6471118927001953,\n",
       "  2.4920494556427,\n",
       "  -0.7584431171417236,\n",
       "  -0.835055410861969,\n",
       "  1.3479602336883545,\n",
       "  -4.635622024536133,\n",
       "  0.021054772660136223,\n",
       "  -1.8994276523590088,\n",
       "  -1.6807500123977661,\n",
       "  -3.576667547225952,\n",
       "  1.2977772951126099,\n",
       "  ...]}"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ollama.embeddings(model='llama3.1', prompt='天空是蓝色的因为瑞利散射')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5b92fd1b",
   "metadata": {},
   "source": [
    "### 进程\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "d9caab17",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'models': [{'name': 'llama3.1:latest',\n",
       "   'model': 'llama3.1:latest',\n",
       "   'size': 6654289920,\n",
       "   'digest': '75382d0899dfaaa6ce331cf680b72bd6812c7f05e5158c5f2f43c6383e21d734',\n",
       "   'details': {'parent_model': '',\n",
       "    'format': 'gguf',\n",
       "    'family': 'llama',\n",
       "    'families': ['llama'],\n",
       "    'parameter_size': '8.0B',\n",
       "    'quantization_level': 'Q4_0'},\n",
       "   'expires_at': '2024-08-09T17:51:17.3304647+08:00',\n",
       "   'size_vram': 6654289920}]}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ollama.ps()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "15d7fedf",
   "metadata": {},
   "source": [
    "## 自定义客户端\n",
    "\n",
    "可以使用以下字段创建自定义客户端：\n",
    "\n",
    "- `host`: 要连接的 Ollama 主机\n",
    "- `timeout`: 请求超时时间\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "33349ab6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'model': 'llama3.1',\n",
       " 'created_at': '2024-08-09T09:50:33.090211856Z',\n",
       " 'message': {'role': 'assistant', 'content': '因为大气中有很多氦和氮，散射了光。'},\n",
       " 'done_reason': 'stop',\n",
       " 'done': True,\n",
       " 'total_duration': 8818670948,\n",
       " 'load_duration': 8350474275,\n",
       " 'prompt_eval_count': 18,\n",
       " 'prompt_eval_duration': 30688000,\n",
       " 'eval_count': 18,\n",
       " 'eval_duration': 395311000}"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from ollama import Client\n",
    "client = Client(host='http://localhost:11434')\n",
    "response = client.chat(model='llama3.1', messages=[\n",
    "  {\n",
    "    'role': 'user',\n",
    "    'content': '为什么天空是蓝色的？',\n",
    "  },\n",
    "])\n",
    "response"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ba7a1dc0",
   "metadata": {},
   "source": [
    "## 异步客户端\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "f89276cd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'model': 'llama3.1', 'created_at': '2024-08-09T09:55:42.591365652Z', 'message': {'role': 'assistant', 'content': '答案：因为大气中的分子和气体散射光。\\n\\n天空为什么是蓝色的，这个问题可能已经有很多年了，但是你知道吗？天空的颜色不是因为天空本身，而是因为我们看到的大气在散射光。所以，实际上，没有天空，它们只是我们的眼睛看到大气和太阳一起产生的景象。\\n\\n当阳光穿过地球的大气时，它会遇到大量的气体分子，这些气体分子会散射光。这就像你在深海中看到了闪亮的水滴一样。当光从天空中照进你的眼睛时，所有来自大气和太阳的各个色调都会混合在一起，最后变成了蓝色。\\n\\n因此，我们可以说是，大气中的分子和气体散射光造成了我们看到的蓝色的天空。'}, 'done_reason': 'stop', 'done': True, 'total_duration': 7331322929, 'load_duration': 2913727677, 'prompt_eval_count': 18, 'prompt_eval_duration': 29622000, 'eval_count': 190, 'eval_duration': 4327579000}\n"
     ]
    }
   ],
   "source": [
    "import asyncio\n",
    "from ollama import AsyncClient\n",
    "import nest_asyncio\n",
    "\n",
    "nest_asyncio.apply()\n",
    "\n",
    "async def chat():\n",
    "    message = {'role': 'user', 'content': '为什么天空是蓝色的？'}\n",
    "    response = await AsyncClient().chat(model='llama3.1', messages=[message])\n",
    "    print(response)\n",
    "\n",
    "asyncio.run(chat())\n"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "732033c2",
   "metadata": {},
   "source": [
    "设置 `stream=True` 修改函数以返回 Python 异步生成器：\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "b83f1c19",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "答案：这是由于地球大气层中散射光的性质所致。我们知道，阳光中的各种色彩都有其对应的波长，蓝色光线的波长最短。因为蓝色光线能穿过大气层的空气分子，而不被吸收，所以它能够到达我们的眼睛里，因此天空看起来是蓝色的。"
     ]
    }
   ],
   "source": [
    "import asyncio\n",
    "from ollama import AsyncClient\n",
    "import nest_asyncio\n",
    "\n",
    "nest_asyncio.apply()\n",
    "async def chat():\n",
    "  message = {'role': 'user', 'content': '为什么天空是蓝色的？'}\n",
    "  async for part in await AsyncClient().chat(model='llama3.1', messages=[message], stream=True):\n",
    "    print(part['message']['content'], end='', flush=True)\n",
    "\n",
    "asyncio.run(chat())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6a1dc7c9",
   "metadata": {},
   "source": [
    "## 错误\n",
    "\n",
    "如果请求返回错误状态或在流式传输时检测到错误，则会引发错误。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "edc5ab3b",
   "metadata": {},
   "outputs": [],
   "source": [
    "model = 'does-not-yet-exist'\n",
    "\n",
    "try:\n",
    "  ollama.chat(model)\n",
    "except ollama.ResponseError as e:\n",
    "  print('错误:', e.error)\n",
    "  if e.status_code == 404:\n",
    "    ollama.pull(model)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "ollama",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
